import numpy as np
import cv2
import os
import pickle
import random
from tqdm import tqdm
from .build import DATASET_OUTPUT_DIR, DATASET_CONFIG_DIR, DATASET_REGISTRY, DATASET_INFO_REGISTRY
from .utils import get_dataset_info_cfg, setup_dataset_info, convert_to_one_category, map_category, get_plugin_annotations

DATASET = 'KAIST'
DATASET_BASE_DIR = 'dataset/KAIST'


CLASSES_LIST = ['cyclist', 'people', 'person', 'person?', 'car', 'person?a']
CLASSES_INDEX = dict()
for i, class_name in enumerate(CLASSES_LIST):
    CLASSES_INDEX[class_name] = i
FAKE_CLASSES_LIST = ['person', 'car']

map_dict = {
    0: 0,
    1: 0,
    2: 0,
    3: 0,
    4: 1,
    5: 0,
}

def get_KAIST_dicts(DATASET_BASE_DIR, image_set_name):
    dataset_dicts = []
    image_set_path = os.path.join(DATASET_BASE_DIR, 'imageSets', image_set_name+'.txt')
    annotations_dir = os.path.join(DATASET_BASE_DIR, 'annotations')

    with open(image_set_path, 'r') as f:
        image_id_list = [line.strip() for line in f.readlines()]

    for image_id in tqdm(image_id_list):
        annotations_full_path = os.path.join(annotations_dir, image_id + '.txt')

        image_id_split = image_id.strip().split('/')
        image_full_path = os.path.join(DATASET_BASE_DIR, 'images', image_id_split[0], image_id_split[1], 'lwir', image_id_split[2])
        img = None
        for suffix in ['.png', '.jpg']:
            img = cv2.imread(image_full_path + suffix)
            if img is not None:
                image_full_path = image_full_path + suffix
                break
        if img is None:
            raise FileNotFoundError("Can not load image " + image_full_path)

        height, width, channels = img.shape

        with open(annotations_full_path, 'r', encoding='utf-8') as f:
            object_annotation_list = f.readlines()[1:]

        objects_list = []
        for object_annotation in object_annotation_list:
            line = object_annotation.split()
            label = line[0]
            bounding_box = [float(item.strip()) for item in line[1:5]]
            bounding_box[2], bounding_box[3] = bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]
            bounding_box = [max(bounding_box[0], 0.0), max(bounding_box[1], 0.0), min(bounding_box[2], width - 1.0), min(bounding_box[3], height - 1.0)]
            object_dict = {
                'label': label,
                'bounding_box': bounding_box
            }
            objects_list.append(object_dict)

        # to uniform interface
        objects = []
        for object in objects_list:
            object_dict = {
                'bbox': object['bounding_box'],
                'category_id': CLASSES_INDEX[object['label']]
            }
            objects.append(object_dict)

        record = {
            'file_name': image_full_path,
            'image_id': image_id,
            'annotations': objects,
            'params': {
                'height': height,
                'width': width,
            }
        }
        dataset_dicts.append(record)

    return dataset_dicts

def get_KAIST_clear_dicts(DATASET_BASE_DIR, image_set_name):
    dataset_dicts = []
    image_set_path = os.path.join(DATASET_BASE_DIR, 'clear_annotations', 'imageSets', image_set_name+'.txt')
    if image_set_name == 'train':
        annotations_dir = os.path.join(DATASET_BASE_DIR, 'clear_annotations', 'sanitized_annotations')
    elif image_set_name == 'test':
        annotations_dir = os.path.join(DATASET_BASE_DIR, 'clear_annotations', 'annotations_KAIST_test_set')
    else:
        raise NotImplementedError("unknown dataset name " + image_set_name)

    with open(image_set_path, 'r') as f:
        image_id_list = [line.strip() for line in f.readlines()]

    for image_id in tqdm(image_id_list):
        annotations_full_path = os.path.join(annotations_dir, image_id.replace('/', '_') + '.txt')

        image_id_split = image_id.strip().split('/')
        image_full_path = os.path.join(DATASET_BASE_DIR, 'images', image_id_split[0], image_id_split[1], 'lwir', image_id_split[2])
        img = None
        for suffix in ['.png', '.jpg']:
            img = cv2.imread(image_full_path + suffix)
            if img is not None:
                image_full_path = image_full_path + suffix
                break
        if img is None:
            raise FileNotFoundError("Can not load image " + image_full_path)

        height, width, channels = img.shape

        with open(annotations_full_path, 'r', encoding='utf-8') as f:
            object_annotation_list = f.readlines()[1:]

        objects_list = []
        for object_annotation in object_annotation_list:
            line = object_annotation.split()
            label = line[0]
            bounding_box = [float(item.strip()) for item in line[1:5]]
            bounding_box[2], bounding_box[3] = bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]
            bounding_box = [max(bounding_box[0], 0.0), max(bounding_box[1], 0.0), min(bounding_box[2], width - 1.0), min(bounding_box[3], height - 1.0)]
            object_dict = {
                'label': label,
                'bounding_box': bounding_box
            }
            objects_list.append(object_dict)

        # to uniform interface
        objects = []
        for object in objects_list:
            object_dict = {
                'bbox': object['bounding_box'],
                'category_id': CLASSES_INDEX[object['label']]
            }
            objects.append(object_dict)

        record = {
            'file_name': image_full_path,
            'image_id': image_id,
            'annotations': objects,
            'params': {
                'height': height,
                'width': width,
            }
        }
        dataset_dicts.append(record)

    return dataset_dicts

def remove_too_small(dataset_dicts, area_threshold):
    for i in range(len(dataset_dicts)):
        objs = []
        for obj in dataset_dicts[i]['annotations']:
            area = (obj['bbox'][2] - obj['bbox'][0]) * (obj['bbox'][3] - obj['bbox'][1])
            if area >= area_threshold:
                objs.append(obj)
        dataset_dicts[i]['annotations'] = objs
    return dataset_dicts

def crop_dataset_dicts(dataset_dicts, size):
    """ size: (w, h) """
    random.seed(0)
    new_dataset_dicts = []
    for dataset_dict in dataset_dicts:
        h, w = dataset_dict['params']['height'], dataset_dict['params']['width']
        new_w, new_h = size
        x = int(random.random() * (w - new_w))
        y = int(random.random() * (h - new_h))

        clamp = lambda x, lower, upper: min(max(x, lower), upper)
        
        new_objects = []
        for obj in dataset_dict['annotations']:
            inter_w = max(min(x+new_w, obj['bbox'][2]) - max(x, obj['bbox'][0]), 0)
            inter_h = max(min(y+new_h, obj['bbox'][3]) - max(y, obj['bbox'][1]), 0)
            inter_area_ratio = inter_h * inter_w / ((obj['bbox'][3] - obj['bbox'][1]) * (obj['bbox'][2] - obj['bbox'][0]))
            if inter_area_ratio < 0.5:
                continue

            obj['bbox'][0] = clamp(obj['bbox'][0] - x, 0, new_w)
            obj['bbox'][1] = clamp(obj['bbox'][1] - y, 0, new_h)
            obj['bbox'][2] = clamp(obj['bbox'][2] - x, 0, new_w)
            obj['bbox'][3] = clamp(obj['bbox'][3] - y, 0, new_h)
            obj['bbox'] = [float(i) for i in obj['bbox']]
            new_objects.append(obj)
        dataset_dict['annotations'] = new_objects
        dataset_dict['params']['height'] = new_h
        dataset_dict['params']['width'] = new_w
        dataset_dict['params']['crop'] = [x, y, x+new_w, y+new_h]
        new_dataset_dicts.append(dataset_dict)
    return new_dataset_dicts


def select_classes(dataset_dicts, classes_list):
    classes_list = [CLASSES_INDEX[label] for label in classes_list]
    new_dataset_dicts = []
    for dataset_dict in dataset_dicts:
        new_object_list = []
        for object_dict in dataset_dict["annotations"]:
            if object_dict['category_id'] in classes_list:
                new_object_list.append(object_dict)
        dataset_dict['annotations'] = new_object_list
        new_dataset_dicts.append(dataset_dict)
    return new_dataset_dicts

def remove_empty(dataset_dicts):
    dataset_dicts = [dataset_dict for dataset_dict in dataset_dicts if len(dataset_dict['annotations']) > 0]
    return dataset_dicts

def add_plugin_annotations(dataset_dicts, plugin_annotations_file):
    annotations_dict = get_plugin_annotations(plugin_annotations_file)
    for i in range(len(dataset_dicts)):
        filename = dataset_dicts[i]['file_name']
        if filename in annotations_dict:
            dataset_dicts[i]['annotations'] += annotations_dict[filename]
    return dataset_dicts

def remove_plugin_annotations(dataset_dicts, plugin_annotations_file):
    annotations_dict = get_plugin_annotations(plugin_annotations_file)
    new_dataset_dicts = []
    for dataset_dict in dataset_dicts:
        filename = dataset_dict['file_name']
        if not (filename in annotations_dict and len(annotations_dict[filename]) > 0):
            new_dataset_dicts.append(dataset_dict)
    return new_dataset_dicts

def bind_dataset_type(dataset_dicts, dataset_type):
    for i in range(len(dataset_dicts)):
        dataset_dicts[i]['params']['dataset_type'] = dataset_type
    return dataset_dicts

def get_dataset_dicts(dataset_name, **kwargs):
    available_set = set(['sep', 'crop', 'remove_empty', 'add_plugin', 'remove_plugin', 'replace_plugin', 'bind_dataset_type', 'rm_small', 'clear'])
    assert set(kwargs.keys()).issubset(available_set), 'Invalid kwargs'

    content = ''
    if 'sep' in kwargs:
        content += '-[sep]-' + '-'.join(kwargs['sep']['classes_list'])
    if 'crop' in kwargs:
        content += '-[crop]-' + '-'.join([str(i) for i in kwargs['crop']['size']])
    if 'remove_empty' in kwargs:
        content += '-[remove_empty]'
    if 'rm_small' in kwargs:
        content += '-[rm_small]-' + str(kwargs['rm_small']['area_threshold'])
    if 'add_plugin' in kwargs:
        content += '-[add_plu]-{}'.format(hashlib.blake2b(bytes(kwargs['add_plugin']['file'], encoding='utf-8'), digest_size=2).hexdigest())
    if 'remove_plugin' in kwargs:
        content += '-[rm_plu]-{}'.format(hashlib.blake2b(bytes(kwargs['remove_plugin']['file'], encoding='utf-8'), digest_size=2).hexdigest())
    if 'bind_dataset_type' in kwargs:
        content += '-[bdt]-' + str(kwargs['bind_dataset_type']['dataset_type'])
    if 'clear' in kwargs:
        content += '-[clear]'
    pickle_file_name = os.path.join(DATASET_OUTPUT_DIR, DATASET + '-' + dataset_name + content + '-dataset.pkl')
    dataset_dicts = None
    if os.path.exists(pickle_file_name):
        with open(pickle_file_name, 'rb') as f:
            dataset_dicts = pickle.load(f)
    else:
        if 'bind_dataset_type' in kwargs:
            params = kwargs.pop('bind_dataset_type')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = bind_dataset_type(dataset_dicts, params['dataset_type'])
        elif 'remove_empty' in kwargs:
            kwargs.pop('remove_empty')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = remove_empty(dataset_dicts)
        elif 'crop' in kwargs:
            params = kwargs.pop('crop')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = crop_dataset_dicts(dataset_dicts, params['size'])
        elif 'sep' in kwargs:
            params = kwargs.pop('sep')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = select_classes(dataset_dicts, params['classes_list'])
        elif 'rm_small' in kwargs:
            params = kwargs.pop('rm_small')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = remove_too_small(dataset_dicts, params['area_threshold'])
        elif 'add_plugin' in kwargs:
            params = kwargs.pop('add_plugin')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = add_plugin_annotations(dataset_dicts, params['file'])
        elif 'remove_plugin' in kwargs:
            params = kwargs.pop('remove_plugin')
            dataset_dicts = get_dataset_dicts(dataset_name, **kwargs)
            dataset_dicts = remove_plugin_annotations(dataset_dicts, params['file'])
        elif 'clear' in kwargs:
            params = kwargs.pop('clear')
            if dataset_name.startswith('train'):
                dataset_name = 'train'
            elif dataset_name.startswith('test'):
                dataset_name = 'test'
            dataset_dicts = get_KAIST_clear_dicts(DATASET_BASE_DIR, dataset_name)
        else:
            assert len(kwargs) == 0
            dataset_dicts = get_KAIST_dicts(DATASET_BASE_DIR, dataset_name)
        os.makedirs(os.path.dirname(pickle_file_name), exist_ok=True)
        with open(pickle_file_name, 'wb') as f:
            pickle.dump(dataset_dicts, f)
    return dataset_dicts

dataset_name_train = 'train01'
dataset_name_test = 'test01'

DATASET_INFO_REGISTRY.register(DATASET+'-g', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-person', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-person-remove-small-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-crop2', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g-crop.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-crop2-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g-crop.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-crop2-add-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g-crop.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-crop2-rm-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g-crop.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-remove-empty-t0', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-add-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-g-rm-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-clear-person-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-person-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-person-car-add-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))
DATASET_INFO_REGISTRY.register(DATASET+'-sep-g-person-car-rm-plugin-remove-empty', get_dataset_info_cfg(os.path.join(DATASET_CONFIG_DIR, 'KAIST-g.yaml')))

for postfix, kwargs in {
        '-g': {},
        '-sep-g-person': {
            'sep': {'classes_list': ['person']}
        },
        '-sep-g-person-remove-small-empty': {
            'sep': {'classes_list': ['person']},
            'rm_small': {
                'area_threshold': 625,
            },
            'remove_empty': None,
        },
        '-g-crop2': {
            'crop': {'size': (300, 256)}
        },
        '-g-crop2-remove-empty': {
            'crop': {'size': (300, 256)},
            'remove_empty': None
        },
        '-g-crop2-add-plugin-remove-empty': {
            'crop': {'size': (300, 256)},
            'remove_empty': None,
            'add_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
        '-g-crop2-rm-plugin-remove-empty': {
            'crop': {'size': (300, 256)},
            'remove_empty': None,
            'remove_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
        '-g-remove-empty': {
            'remove_empty': None
        },
        '-g-remove-empty-t0': {
            'remove_empty': None,
            'bind_dataset_type': {
                'dataset_type': 0,
            },
        },
        '-g-add-plugin-remove-empty': {
            'remove_empty': None,
            'add_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
        '-g-rm-plugin-remove-empty': {
            'remove_empty': None,
            'remove_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
        '-sep-g-clear-person-remove-empty': {
            'remove_empty': None,
            'clear': None,
            'sep': {'classes_list': ['person']},
        },
        '-sep-g-person-remove-empty': {
            'sep': {'classes_list': ['person']},
            'remove_empty': None
        },
        '-sep-g-person-car-add-plugin-remove-empty': {
            'sep': {'classes_list': ['person', 'car']},
            'remove_empty': None,
            'add_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
        '-sep-g-person-car-rm-plugin-remove-empty': {
            'sep': {'classes_list': ['person', 'car']},
            'remove_empty': None,
            'remove_plugin': {
                'file': 'dataset/KAIST-g-plugin-annotations.txt'
            },
        },
    }.items():
    dataset_full_name = DATASET + postfix
    
    DATASET_REGISTRY.register(dataset_full_name +'-train', lambda dataset_name=dataset_name_train, kwargs=kwargs, dataset_info=DATASET_INFO_REGISTRY.get(dataset_full_name): setup_dataset_info(map_category(get_dataset_dicts(dataset_name, **kwargs), map_dict), dataset_info))
    DATASET_REGISTRY.register(dataset_full_name +'-test', lambda dataset_name=dataset_name_test, kwargs=kwargs, dataset_info=DATASET_INFO_REGISTRY.get(dataset_full_name): setup_dataset_info(map_category(get_dataset_dicts(dataset_name, **kwargs), map_dict), dataset_info))
